/********************************************************************************
 * Copyright(c) 2020-2025 VINCENT_WY All rights reserved.
 * - Filename
 * - Author  Vincent
 * - Version V1.0.0
 * - Date    2023/09/21
 * - Brief
 * - FunctionList:
 ******************************************************************************
 * History:
 *
 *
 *
 ******************************************************************************
 */

#pragma once

#include "BlockQueue.hpp"
#include "CameraFrame.hpp"
#include "BevFrame.hpp"
#include "DispatchFile.hpp"
#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <mutex>
#include <string>
#include <thread>
#include <vector>

class DispatchReplayImpl
{
public:
  DispatchReplayImpl(const int &id, const std::string &path);
  ~DispatchReplayImpl();
  void start(const std::function<void(std::shared_ptr<CameraFrame> &&)> &,
             const std::function<void(std::shared_ptr<BevFrame> &&)> &, const long long int &);
  void stop();
  dispatch::FileMap getFile();
  bool isComplete();
  void submit(std::shared_ptr<CameraFrame> frame);
  void doRepeat();
  void doReplay();

private:
  enum class Status
  {
    START = 0,
    REPEAT,
    CONTINUE,
    IDLE
  };

  bool _isExit{false};
  bool _isComplete{false};
  int _cameraId;
  long long int _startTime{0};
  std::string _root{};
  std::mutex _frameMtx;
  std::condition_variable _frameCv;
  std::deque<std::shared_ptr<CameraFrame>> _frameDqu;
  Status _status{Status::IDLE};
  dispatch::FileMap _fileMap;
  std::unique_ptr<std::thread> _replayLoop{nullptr};
  std::unique_ptr<std::thread> _invokeLoop{nullptr};
  BlockQueue<std::shared_ptr<CameraFrame>> _fileInfoQueue{100};
  void ReadFile();
  void ReplayLoop();
  void InvokeLoop(
      const std::function<void(std::shared_ptr<CameraFrame> &&)> &, const std::function<void(std::shared_ptr<BevFrame> &&)> &);
};